import { createStore } from 'vuex'
import { ElMessage } from 'element-plus'
export default createStore({
  state: {
    num: 888,
    tabsList: [],
    conTextTabs: ''
  },
  mutations: {
    //添加tab
    addTabs(state, tab) {
      //如果有 就不添加
      const isSome = state.tabsList.some((item) => {
        return item.path === tab.path
      })
      if (!isSome && tab.path !== 'login') {
        const itemTab = {
          fullPath: tab.fullPath,
          meta: tab.meta,
          path: tab.path,
          name: tab.name,
          query: tab.query,
          params: tab.params,
          title: tab.meta.title
        }
        state.tabsList.push(itemTab)
      }
    },
    //移除tab
    removeTabs(state, tab) {
      const index = state.tabsList.findIndex((item, index) => {
        return item.path == tab.path
      })
      state.tabsList.splice(index, 1)
    },
    //右键时保存当前tab
    saveContext(state, data) {
      state.conTextTabs = data
    },
    //关闭所有
    closeAllTabs(state) {
      state.tabsList = []
      sessionStorage.removeItem('tabs')
    },
    //关闭其他
    closeOtherTabs(state, currenRoute) {
      if (state.conTextTabs == currenRoute) {
        // 右键点击的是当前路由，直接关闭其他所有
        state.tabsList = state.tabsList.filter((item) => {
          return item.path === state.conTextTabs || item.path === '/Home/home'
        })
      } else {
        ElMessage({ message: '请在当前页面关闭', type: 'warning' })
      }
    }
  },
  getters: {
    getAddTab(state) {
      return state.tabs
    }
  },
  actions: {},
  modules: {}
})
